local CSVLoader = require("app.utils.CSVLoader")

local InfoManager = class("InfoManager")
--全局的实例
InfoManager.instance = nil

function InfoManager:ctor(eType)		--GameControlType
--	InfoManager.super.ctor(self, eType)
	self:Init()
end

function InfoManager:GetInstance()
	if InfoManager.instance == nil then
		InfoManager.instance = InfoManager.new()
	end
	return InfoManager.instance
end

function InfoManager:Init()
    self.vRoleInfos = {}

    self:SaveActorInfo()

--	self.vKingInfos = {}
--	self.vKingLevelInfos = {}
--	self.vHeroInfos = {}
--	self.vHeroLevelInfos = {}
--	self.vMonsterInfos = {}
--	self.vMonBuyInfos = {}		-- 战斗中可买的怪物
--	self.vSoldierInfos = {}
--    self.vRaceInfos = {}

--	self:SaveKingInfo()
--	self:SaveHeroInfo()
--	self:SaveMonsterInfo()
--	self:SaveMonBuyInfo()
--	self:SaveSoldierInfo()
--    self:SaveRaceInfo()
end

function InfoManager:SaveActorInfo()
    local vData = CSVLoader.loadCSV("Config/role.csv")
	for i,v in ipairs(vData) do        
		self.vRoleInfos[v.id] = v
	end
end

function InfoManager:GetRole(id)
    if id then return self.vRoleInfos[id]
    end
    return self.vRoleInfos
end


function InfoManager:ClearValue()
end

function InfoManager:MakeProperties(info)
    -- 属性
    local properties = { }
    local propNames = { "hp", "attack", "def", "move_speed", "attack_speed", "crit_rate", "dodge_rate" }
    local propTypes = {
        td.Property.HP,td.Property.Atk,td.Property.Def,td.Property.Speed,
        td.Property.AtkSp,td.Property.Crit,td.Property.Dodge
    }
    for i, propName in ipairs(propNames) do
        local _value = info[propName] or 0
        if type(_value) == "string" then
            local tmp = string.split(_value, "#")
            properties[propTypes[i]] = { value = tonumber(tmp[1]), ratio = tonumber(tmp[2]) }
        else
            properties[propTypes[i]] = tonumber(_value)
        end
    end

    properties[td.Property.SuckHp] = 0
    properties[td.Property.Reflect] = 0

    return properties
end

function InfoManager:SaveKingInfo()
	local vInfo = CSVLoader.loadCSV("Config/king.csv")
	for i,v in ipairs(vInfo) do
        v.name = g_LM:getBy(v.name)
        v.desc = g_LM:getBy(v.desc)
        -- 属性
        v.property = self:MakeProperties(v)

        local skillsStr = string.split(v.skill,"#")
        v.skill = {}
        for j, var in ipairs(skillsStr) do
            table.insert(v.skill,tonumber(var))
        end

		self.vKingInfos[v.id] = v
	end
	
	local vLevelData = CSVLoader.loadCSV("Config/king_levelup.csv")
	for i,v in ipairs(vLevelData) do
		self.vKingLevelInfos[v.id] = v
	end
end

function InfoManager:SaveHeroInfo()
	local vHeroData = CSVLoader.loadCSV("Config/hero.csv")
	for i,v in ipairs(vHeroData) do
        v.name = g_LM:getBy(v.name)
        -- 属性
        v.property = self:MakeProperties(v)

        local skillsStr = string.split(v.skill,"#")
        v.skill = {}
        for j, var in ipairs(skillsStr) do
            table.insert(v.skill,tonumber(var))
        end
		
		-- 解锁消耗
		v.unlock = self:_ParseItem(v.unlock)
        -- 升星消耗
        local tmp = string.split(v.star_cost, ";")
        v.star_cost = {}
        for j, v1 in ipairs(tmp) do
			local cost = self:_ParseItem(v1)
            table.insert(v.star_cost, cost)
        end

		self.vHeroInfos[v.id] = v
	end
	
	local vLevelData = CSVLoader.loadCSV("Config/hero_levelup.csv")
	for i,v in ipairs(vLevelData) do
		self.vHeroLevelInfos[v.id] = v
	end
end

function InfoManager:_ParseItem(var)
	local result = { }
	local tmp1 = string.split(var, "|")
	for k, v2 in ipairs(tmp1) do
		local tmp2 = string.split(v2, "#")
		if tonumber(tmp2[1]) == td.ItemID.Gold then
			result.gold = tonumber(tmp2[2])
		else
			result.itemId = tonumber(tmp2[1])
			result.num = tonumber(tmp2[2])
		end
	end

	return result
end

function InfoManager:SaveMonsterInfo()
	local vMonster = CSVLoader.loadCSV("Config/monster.csv")
	for i,v in ipairs(vMonster) do
        v.name = g_LM:getBy(v.name)
        v.desc = g_LM:getBy(v.desc)
        -- 属性
        v.property = self:MakeProperties(v)

        local skillsStr = string.split(v.skill,"#")
        v.skill = {}
        for j, var in ipairs(skillsStr) do
            table.insert(v.skill,tonumber(var))
        end

		self.vMonsterInfos[v.id] = v
	end
end

function InfoManager:SaveMonBuyInfo()
	local vMonster = CSVLoader.loadCSV("Config/monster_buy.csv")
	for i,v in ipairs(vMonster) do
		self.vMonBuyInfos[i] = v
	end
end

function InfoManager:SaveSoldierInfo()
	local vData = CSVLoader.loadCSV("Config/role_detail.csv")
	for i,v in ipairs(vData) do
        v.name = g_LM:getBy(v.name)
        v.desc = g_LM:getBy(v.desc)
        v.ro_desc = g_LM:getBy(v.ro_desc)
        v.profile = g_LM:getBy(v.profile)
        -- 属性
        v.property = self:MakeProperties(v)
		
		local tmp = string.split(v.next_id,"#")
		v.next_id = {}
		for _, idStr in ipairs(tmp) do
			table.insert(v.next_id,tonumber(idStr))
		end
		
        local skillsStr = string.split(v.skill,"#")
        v.skill = {}
        for j, var in ipairs(skillsStr) do
            table.insert(v.skill,tonumber(var))
        end

		self.vSoldierInfos[v.id] = v
	end
end

function InfoManager:SaveRaceInfo()
	local vData = CSVLoader.loadCSV("Config/race.csv")
	for i,v in ipairs(vData) do
        v.soldier = {}
        for i=1,6  do
            local soldierText = "soldier_"..i
            v.soldier[i] = v[soldierText] 
            v[soldierText] = nil
        end
        
		self.vRaceInfos[v.id] = v
	end
end

function InfoManager:GetKingInfo( id )
	return self.vKingInfos[id]
end

function InfoManager:GetKingLevelInfo( id )
	return self.vKingLevelInfos[id]
end

function InfoManager:GetHeroInfo( id )
	if id then
		return self.vHeroInfos[id]
	end
	return self.vHeroInfos
end

function InfoManager:GetHeroLevelInfo( id )
	return self.vHeroLevelInfos[id]
end

function InfoManager:GetMonsterInfo( id )
	if id then
		return self.vMonsterInfos[id]
	end
	return self.vMonsterInfos
end

function InfoManager:GetMonBuyInfo( id )
	if id then
		return self.vMonBuyInfos[id]
	end
	return self.vMonBuyInfos
end

function InfoManager:GetSoldierInfos()
	return self.vSoldierInfos
end

function InfoManager:GetSoldierInfo( id )
	return self.vSoldierInfos[id]
end

function InfoManager:GetRaceInfos()
	return self.vRaceInfos
end

function InfoManager:GetRaceInfo( id )
    return self.vRaceInfos[id]
end

return InfoManager